import { createRouter, createWebHashHistory } from "vue-router";
import { useUserStore } from "@/store/module/user";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

import {
  HomeOutlined,
  UserOutlined,
  UnorderedListOutlined,
  QqOutlined,
  AndroidOutlined,
  ApiOutlined,
  HistoryOutlined
} from "@ant-design/icons-vue";
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      component: () => import("@/components/Layout.vue"),
      redirect: "/home",
      children: [
        {
          path: "home",
          component: () => import("@/view/Home.vue"),
          icon: HomeOutlined,
          title: "首页",
        },
        // {
        //   path: "user",
        //   component: () => import("@/view/User.vue"),
        //   icon: UserOutlined,
        //   title: "用户",
        // },
        {
          path: "PatientBind",
          component: () => import("@/view/PatientBind.vue"),
          icon: ApiOutlined,
          title: "新患者设备绑定",
        },
        {
          path: "HistoryQuery",
          component: () => import("@/view/HistoryQuery.vue"),
          icon: HistoryOutlined,
          title: "历史记录查询",
        },
        // {
        //   path: "option",
        //   // component: () => import("@/view/Option.vue"),
        //   icon: UnorderedListOutlined,
        //   title: "选项",
        //   redirect: "/option/PatientBind",
        //   children: [
        //     {
        //       path: "PatientBind",
        //       component: () => import("@/view/PatientBind.vue"),
        //       icon: QqOutlined,
        //       title: "新患者设备绑定",
        //     },
        //     {
        //       path: "HistoryQuery",
        //       component: () => import("@/view/HistoryQuery.vue"),
        //       icon: AndroidOutlined,
        //       title: "选项二",
        //     },
        //   ],
        // },
      ],
    },
    {
      path: "/login",
      component: () => import("@/view/Login.vue"),
    },
    {
      path: "/pressureMonitor",
      component: () => import("@/view/pressureMonitor.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  NProgress.start();
  if (to.path === "/login") {
    next();
    NProgress.done();
  } else {
    const token = userStore.token;
    if (token) {
      next();
    } else {
      userStore.clearUserInfo();
      userStore.menuKeys = ["home"];
      next("/login");
    }
    NProgress.done();
  }
});
export default router;
